package wymiana.mbeans.group;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import wymiana.domain.DGroup;
import wymiana.domain.Department;
import wymiana.domain.User;
import wymiana.mbeans.UserBean;
import wymiana.service.DepartmentService;
import wymiana.service.GroupService;
import wymiana.service.SchoolService;

@ManagedBean(name = "groupMBean")
// @RequestScoped
@SessionScoped
public class GroupMBean implements Serializable {

	/** serialization uid */
	private static final long serialVersionUID = 869956988165109750L;

	private List<DGroup> groups = null;

	private List<DGroup> myGroups = null;

	private List<DGroup> filteredGroups = null;

	/** aktualnie wybrana uczelnia */
	private DGroup actualGroup = null;

	@ManagedProperty("#{departmentService}")
	private DepartmentService departmentService;

	@ManagedProperty("#{userMBean}")
	private UserBean userMBean;

	@ManagedProperty("#{schoolService}")
	private SchoolService schoolService;

	@ManagedProperty("#{groupService}")
	private GroupService groupService;

	private String choosenId = null;

	public GroupMBean() {
		// TODO inicjalizacja MBeana

	}

	public boolean isInGroup() {
		User u = userMBean.getLoggedUser();

		for (User a : actualGroup.getUsers()) {
			if (a.getUsername().equals(u.getUsername())) {
				return true;
			}
		}

		if (u.getUsername().equals(actualGroup.getCreateUser().getUsername())) {
			return true;
		}

		return false;
	}

	public String goToGroups() {
		return "group";
	}

	public String goToMyGroup() {
		return "myGroup";
	}

	public String editGroup() {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();

		String groupId = (String) externalContext.getRequestParameterMap().get(
				"groupId");
		actualGroup = groupService.getGroup(Integer.parseInt(groupId));
		choosenId = actualGroup.getDepartment().getIdDepartment().toString();
		return "editGroup";
	}

	public String addGroup() {
		actualGroup = new DGroup();
		return "addGroup";
	}

	public String removeGroup() {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();

		String groupId = (String) externalContext.getRequestParameterMap().get(
				"groupId");

		// School s = schoolService.getSchool(Integer.parseInt(schoolId));
		setGroups(null);

		groupService.deleteGroupById(Integer.parseInt(groupId));
		return "";
	}

	public String showGroup() {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();

		String groupId = (String) externalContext.getRequestParameterMap().get(
				"groupId");
		actualGroup = groupService.getGroup(Integer.parseInt(groupId));
		choosenId = actualGroup.getDepartment().getIdDepartment().toString();

		return "showGroup";
	}

	public String saveGroup() {

		if (actualGroup.getIdGroup() == null) {
			User u = userMBean.getLoggedUser();
			actualGroup.setCreateUser(u);
			actualGroup.setCreationDate(new Date());
			Department d = departmentService.getDepartment(Integer
					.parseInt(choosenId));
			actualGroup.setDepartment(d);
			groupService.create(actualGroup);
		} else {
			Department d = departmentService.getDepartment(Integer
					.parseInt(choosenId));
			actualGroup.setDepartment(null);
			this.groupService.updateGroup(actualGroup);
			actualGroup.setDepartment(d);
			this.groupService.updateGroup(actualGroup);
		}

		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				"Sukces: ", "Zapisano zmiany");

		FacesContext.getCurrentInstance().addMessage(null, message);
		boolean contains = false;
		DGroup toDel = null;
		for (DGroup d : getGroups()) {
			if (d.getIdGroup().equals(actualGroup.getIdGroup())) {
				contains = true;
				toDel = d;
				break;
			}
		}

		if (!contains) {
			getGroups().add(actualGroup);
			actualGroup = null;
		} else {
			getGroups().remove(toDel);
			getGroups().add(actualGroup);
		}

		return "group";
	}

	public String saveEditedGroup() {
		saveGroup();
		return "";
	}

	/**
	 * @return the schools
	 */
	public List<DGroup> getGroups() {
		if (groups == null) {
			groups = groupService.getAllGroups();
		}
		return groups;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	/**
	 * @return the choosenId
	 */
	public String getChoosenId() {
		return choosenId;
	}

	/**
	 * @param choosenId
	 *            the choosenId to set
	 */
	public void setChoosenId(String choosenId) {
		this.choosenId = choosenId;
	}

	/**
	 * @return the schoolService
	 */
	public SchoolService getSchoolService() {
		return schoolService;
	}

	/**
	 * @param schoolService
	 *            the schoolService to set
	 */
	public void setSchoolService(SchoolService schoolService) {
		this.schoolService = schoolService;
	}

	public DGroup getActualGroup() {
		return actualGroup;
	}

	public void setActualGroup(DGroup actualGroup) {
		this.actualGroup = actualGroup;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public void setGroups(List<DGroup> groups) {
		this.groups = groups;
	}

	/**
	 * @return the filteredGroups
	 */
	public List<DGroup> getFilteredGroups() {
		return filteredGroups;
	}

	/**
	 * @param filteredGroups
	 *            the filteredGroups to set
	 */
	public void setFilteredGroups(List<DGroup> filteredGroups) {
		this.filteredGroups = filteredGroups;
	}

	/**
	 * @return the userMBean
	 */
	public UserBean getUserMBean() {
		return userMBean;
	}

	/**
	 * @param userMBean
	 *            the userMBean to set
	 */
	public void setUserMBean(UserBean userMBean) {
		this.userMBean = userMBean;
	}

	/**
	 * @return the myGroups
	 */
	public List<DGroup> getMyGroups() {

		myGroups = groupService.getMyGroups(userMBean.getLoggedUser());
		return myGroups;
	}

	/**
	 * @param myGroups
	 *            the myGroups to set
	 */
	public void setMyGroups(List<DGroup> myGroups) {
		this.myGroups = myGroups;
	}

}
